// pages/franchiseees/franchiseeeListEdit/franchiseeeListEdit.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow1: false,
    isShow2: false,
    isShow3: false,
    searchImg: '/images/add1.png',
    pageType: 2, //页面类型1:添加2:编辑3:查看
    statusList: [{
        id: 1,
        label: '正常'
      },
      {
        id: 2,
        label: '禁用'
      }
    ], //用户状态
    statusIndex: 0, //用户状态选择
    id: 0,
    userInfo: [],
    account_name: '',
    password: '',
    rpassword: '',
    tel: '',
    ID_card: '',
    franchiser_proportion: 0,
    franchiser_recharge_proportion: 0, //充值卡分成比例
    address: '',
    account: '',
    account_recharge_balance: 0,
    special_recharge_balance: 0,
    promotion_balance: 0, //促销金额
    account_recharge_balance_add: 0,
    special_recharge_balance_add: 0,
    promotion_recharge_balance_add: 0,
    tapCheck: true,
    is_self: 1, //是否自营 1自营 2非自营
    promotion_proportion: 0, //返促销金比例
    isSelf: false, //合伙人是自营的情况只能选择自营
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var pageType = options.type;
    var id = options.id;


    //合伙人是自营的情况只能选择自营
    this.getPartner();

    if (pageType == 1) {
      wx.setNavigationBarTitle({
        title: '添加加盟商'
      })
    } else if (pageType == 2) {
      wx.setNavigationBarTitle({
        title: '加盟商编辑'
      })
      this.getPartnerInfo(id);
    } else if (pageType == 3) {
      wx.setNavigationBarTitle({
        title: '加盟商详情'
      })
      this.getPartnerInfo(id);
    }

    this.setData({
      pageType: pageType,
    })
  },


  getPartner() {
    app.ajax({
      url: "Partner/User/getPartnerInfo",
      success: res => {
        if (res.data.code > 0) {
          this.setData({
            isSelf: res.data.data.is_self == 1 ? true : false
          })
        } else {
          app.showToast(res.data.message);
        }
      }
    })
  },


  /**
   * 选择加盟商状态
   */
  statusChange: function (e) {
    this.setData({
      statusIndex: e.detail.value
    })
  },

  //名称
  franName(e) {
    this.setData({
      account_name: e.detail.value
    })
  },
  //账号
  franAccount(e) {
    this.setData({
      account: e.detail.value
    })
  },
  //密码
  franPassword(e) {
    this.setData({
      password: e.detail.value
    })
  },
  //密码确认
  franPasswordAgain(e) {
    this.setData({
      rpassword: e.detail.value
    })
  },
  //手机号
  franPhone(e) {
    this.setData({
      tel: e.detail.value
    })
  },
  //身份证号
  franIdCard(e) {
    this.setData({
      ID_card: e.detail.value
    })
  },
  //返促销金比例
  promotionPercentage(e) {
    this.setData({
      promotion_proportion: e.detail.value
    })
  },
  //分成比例
  franPercentage(e) {
    this.setData({
      franchiser_proportion: e.detail.value
    })
  },
  //详细地址
  franAddress(e) {
    this.setData({
      address: e.detail.value
    })
  },

  // 选择是否自营
  radioChange(e) {
    this.setData({
      is_self: e.detail.value
    })
  },
  // 促销金额
  promotion(e) {
    console.log(e);
    this.setData({
      promotion_balance: e.detail.value
    })
  },
  // 充值卡分成比例
  franRePercentage(e) {
    this.setData({
      franchiser_recharge_proportion: e.detail.value
    })
  },

  //充值卡账户
  franMoney1(e) {
    console.log(e)
    this.setData({
      isShow1: true
    })
  },
  //充值卡账户
  franMonerInput1(e) {
    this.setData({
      franName: e.detail.value
    })
  },
  //是否保存
  btnChange1(e) {
    console.log(e);
    var that = this;
    var account_recharge_balance = parseFloat(that.data.userInfo.account_recharge_balance);
    var account_recharge_balance_add = parseFloat(that.data.account_recharge_balance_add);
    account_recharge_balance += account_recharge_balance_add;
    that.data.userInfo.account_recharge_balance = account_recharge_balance;

    that.setData({
      isShow: e.detail.btns.isShow,
      account_recharge_balance: account_recharge_balance,
      account_recharge_balance_add: account_recharge_balance_add
    })

  },

  //是否保存
  btnChange3(e) {
    console.log(e);
    var that = this;
    var promotion_balance = parseFloat(that.data.userInfo.promotion_balance);
    var promotion_recharge_balance_add = parseFloat(that.data.promotion_recharge_balance_add);
    promotion_balance += promotion_recharge_balance_add;
    that.data.userInfo.promotion_balance = promotion_balance;

    that.setData({
      isShow: e.detail.btns.isShow,
      promotion_balance: promotion_balance,
      promotion_recharge_balance_add: promotion_recharge_balance_add
    })

  },
  //特殊卡账户
  franMoney2(e) {
    console.log(e)
    this.setData({
      isShow2: true
    })
  },


  franMoney3(e) {
    console.log(e)
    this.setData({
      isShow3: true
    })
  },
  //特殊卡账户
  franMonerInput2(e) {
    this.setData({
      franName: e.detail.value
    })
  },
  //充值卡编辑修改
  franMoner1(e) {
    console.log(e);
    this.setData({
      account_recharge_balance_add: e.detail.value
    })
  },
  //特殊充值卡编辑修改
  franMoner2(e) {
    console.log(e);
    this.setData({
      special_recharge_balance_add: e.detail.value
    })
  },
  franMoner3(e){
    this.setData({
      promotion_recharge_balance_add: e.detail.value
    })
  },

  bind1(e) {
    console.log(e);
    console.log(e);
    this.setData({
      account_recharge_balance: e.detail.value
    })
  },
  bind2(e) {
    console.log(e);
    this.setData({
      special_recharge_balance: e.detail.value
    })
  },
  //是否保存
  btnChange2(e) {
    console.log(e);
    var that = this;
    var special_recharge_balance = parseFloat(that.data.userInfo.special_recharge_balance);
    var special_recharge_balance_add = parseFloat(that.data.special_recharge_balance_add);
    special_recharge_balance += special_recharge_balance_add;
    that.data.userInfo.special_recharge_balance = special_recharge_balance;

    that.setData({
      isShow: e.detail.btns.isShow,
      special_recharge_balance: special_recharge_balance,
      special_recharge_balance_add: special_recharge_balance_add
    })
  },
  //取消添加
  goBack() {
    wx.redirectTo({
      url: '../franchiseeeList/franchiseeeList'
    })
  },
  //保存添加
  addFran() {
    var that = this;
    var account_name = that.data.account_name;
    var account = that.data.account;
    var password = that.data.password;
    var rpassword = that.data.rpassword;
    var tel = that.data.tel;
    var ID_card = that.data.ID_card;
    var franchiser_proportion = that.data.franchiser_proportion;
    var franchiser_recharge_proportion = that.data.franchiser_recharge_proportion;
    var account_status = that.data.statusIndex;
    var address = that.data.address;
    var account_recharge_balance = that.data.account_recharge_balance;
    var special_recharge_balance = that.data.special_recharge_balance;
    var is_self = that.data.is_self;
    var promotion_balance = that.data.promotion_balance;
    var promotion_proportion = that.data.promotion_proportion;

    if (that.data.tapCheck == false) {
      return false;
    }


    if (account_name == '') {
      app.showToast('请输入名称');
      return false;
    }

    if (account == '') {
      app.showToast('请输入账户');
      return false;
    }

    if (password == '' || rpassword == '') {
      app.showToast('请输入密码');
      return false;
    }



    // if (!(/^1(3|4|5|7|8)\d{9}$/.test(tel))) {
    //   app.showToast('请输入手正确手机号码');
    //   return false;
    // }



    if (password) {
      if (password != rpassword) {
        app.showToast('二次密码不一致');
        return false;
      }
    }

    if (that.data.is_self == 1) {
      if (that.data.franchiser_proportion === "") {
        app.showToast('请输入分成比例');
        return false;
      }
      if (that.data.franchiser_recharge_proportion === "") {
        app.showToast('请输入充值卡分成比例');
        return false;
      }
    }

    var account_recharge_balance = that.data.account_recharge_balance;
    var special_recharge_balance = that.data.special_recharge_balance;
    app.ajax({
      url: 'Partner/Franchiser/addFranchiser',
      data: {
        id: that.data.id,
        account: account,
        account_name: account_name,
        password: password,
        tel: tel,
        ID_card: ID_card,
        franchiser_proportion: franchiser_proportion,
        franchiser_recharge_proportion: franchiser_recharge_proportion,
        account_status: account_status,
        address: address,
        account_recharge_balance: account_recharge_balance,
        special_recharge_balance: special_recharge_balance,
        is_self,
        promotion_balance,
        promotion_proportion,
      },
      success: function (res) {
        if (res.data.code == 5000) {
          that.setData({
            tapCheck: false
          })
          app.showToast(res.data.message, "success");
          setTimeout(function () {
            let pages = getCurrentPages();
            let targetPage = pages[pages.length - 2];
            targetPage.reloadData();
            wx.navigateBack({
              delta: 1
            })
          }, 1000);
        } else {
          app.showToast(res.data.message);
          return false;
        }
      }
    })
  },
  //保存编辑
  editFran() {
    console.info('编辑修改提交');
    var that = this;
    var account_name = that.data.account_name;
    var account = that.data.account;
    var password = that.data.password;
    var rpassword = that.data.rpassword;
    var tel = that.data.tel;
    var ID_card = that.data.ID_card;
    var franchiser_proportion = that.data.franchiser_proportion;
    var franchiser_recharge_proportion = that.data.franchiser_recharge_proportion;
    var account_status = that.data.statusIndex;
    var address = that.data.address;
    var account_recharge_balance = that.data.account_recharge_balance;
    var account_recharge_balance_add = that.data.account_recharge_balance_add;
    var special_recharge_balance = that.data.special_recharge_balance;
    var special_recharge_balance_add = that.data.special_recharge_balance_add;
    var is_self = that.data.is_self;
    var promotion_balance = that.data.promotion_balance;
    var promotion_proportion = that.data.promotion_proportion;
    if (that.data.tapCheck == false) {
      return false;
    }
    if (account_name == '') {
      app.showToast('请输入名称');
      return false;
    }

    if (account == '') {
      app.showToast('请输入账户');
      return false;
    }

    if (password) {
      if (password != rpassword) {
        app.showToast('二次密码不一致');
        return false;
      }
    }
    app.ajax({
      url: 'Partner/Franchiser/updateFranchiser',
      data: {
        id: that.data.id,
        account_name: account_name,
        password: password,
        tel: tel,
        ID_card: ID_card,
        franchiser_proportion: franchiser_proportion,
        franchiser_recharge_proportion: franchiser_recharge_proportion,
        account_status: account_status,
        address: address,
        account_recharge_balance: account_recharge_balance,
        account_recharge_balance_add: account_recharge_balance_add,
        special_recharge_balance: special_recharge_balance,
        special_recharge_balance_add: special_recharge_balance_add,
        is_self,
        promotion_balance,
        promotion_proportion,
      },
      success: function (res) {
        if (res.data.code == 5000) {
          app.showToast(res.data.message, "success");
          that.setData({
            tapCheck: false
          })
          setTimeout(function () {
            let pages = getCurrentPages();
            let targetPage = pages[pages.length - 2];
            targetPage.reloadData();
            wx.navigateBack({
              delta: 1
            })
          }, 1000);
        } else {
          app.showToast(res.data.message);
          return false;
        }
      }
    })
  },
  //获取用户信息
  getPartnerInfo(id) {
    var that = this;

    app.ajax({
      url: 'Partner/Franchiser/getPartnerInfo',
      data: {
        id: id
      },
      success: function (res) {
        if (res.data.code == 5000) {
          that.setData({
            userInfo: res.data.data,
            statusIndex: res.data.data.account_status - 1,
            account_name: res.data.data.account_name,
            account: res.data.data.account,
            tel: res.data.data.tel,
            ID_card: res.data.data.ID_card,
            franchiser_proportion: res.data.data.franchiser_proportion * 100,
            franchiser_recharge_proportion: res.data.data.franchiser_recharge_proportion * 100,
            address: res.data.data.address,
            id: res.data.data.id,
            account_recharge_balance: res.data.data.account_recharge_balance,
            special_recharge_balance: res.data.data.special_recharge_balance,
            is_self: res.data.data.is_self,
            promotion_balance: res.data.data.promotion_balance,
            promotion_proportion: res.data.data.promotion_proportion,
          })
        }
      }
    })
  },

})